package com.xingzhe.pengji.security.mapper.provider;

import java.util.Map;

import com.xingzhe.pengji.security.domain.Authorite;
import com.xingzhe.pengji.security.sql.common.Enums;
import com.xingzhe.pengji.security.sql.common.SqlColumnsUtil;
import com.xingzhe.pengji.security.sql.common.SqlKeyWord;

/**
 * @Title ： 三江学院 毕业设计
 * @Description ：
 * @author ：LuFengLiang
 * @Company: IT行者工作室
 * @Copyright: Copyright (c) 2012-2-24
 * @version ： 1.0
 * @param <T>
 */
public class AuthoriteSqlProvider<T extends Authorite> extends BaseSqlProvider<T>
{

    public String updateAuthorite(T entity)
    {
        StringBuilder sb = new StringBuilder();
        sb.append(SqlColumnsUtil.AUTHORITE_UPDATE_TOP_SQL);
        if ( null != entity.getName() )
        {
            sb = buildSelectStringForSet(sb, "name", entity.getName());
        }
        if ( null != entity.getDescription() )
        {
            sb = buildSelectStringForSet(sb, "description", entity.getDescription());
        }
        if ( 0 != entity.getEnabled() )
        {
            sb = buildSelectStringForSet(sb, "enabled", entity.getEnabled());
        }
        if ( 0 != entity.getDelFlag() )
        {
            sb = buildSelectStringForSet(sb, "del_flag", entity.getDelFlag());
        }
        if ( null != entity.getUpdateMan() )
        {
            sb = buildSelectStringForSet(sb, "update_man", entity.getUpdateMan());
        }
        if ( null != entity.getUpdateTime() )
        {
            sb = buildSelectStringForSet(sb, "update_time", entity.getUpdateTime());
        }
        sb.deleteCharAt(sb.lastIndexOf(","));
        sb.append(SqlKeyWord.KeyWord.SPACE.getValue());
        sb.append(SqlKeyWord.KeyWord.WHERE.getValue());
        sb.append(SqlKeyWord.KeyWord.SPACE.getValue());
        sb = buildSelectStringForSet(sb, "id", entity.getId());
        sb.deleteCharAt(sb.lastIndexOf(","));
        return sb.toString();
    }

    @Override
    public StringBuilder dynamicWhereSql(StringBuilder sb, Map<String, Object> map)
    {
        sb.append(SqlKeyWord.KeyWord.SPACE.getValue());
        int id = (Integer) map.get("id");
        if ( id != 0 )
        {
            sb = buildSelectStringForAnd(sb, "id", id, SqlKeyWord.Relationship.EQUAL.name());
        }
        String name = (String) map.get("name");
        if ( null != name )
        {
            sb = buildSelectStringForAnd(sb, "name", name, SqlKeyWord.Relationship.EQUAL.name());
        }
        String description = (String) map.get("description");
        if ( null != description )
        {
            sb.append("and description like '%").append(description).append("%'").append(" ");
        }
        int enabled = (Integer) map.get("enabled");
        if ( enabled != 0 )
        {
            sb = buildSelectStringForAnd(sb, "enabled", enabled, SqlKeyWord.Relationship.EQUAL.name());
        }
        return sb;
    }

    /**
     * 根据权限名称查询Authorite
     */
    public String getAuthoriteByName(String name)
    {
        StringBuilder sb = new StringBuilder();
        sb = getSelectString(sb);
        return buildSelectStringForAnd(sb, "name", name, SqlKeyWord.Relationship.EQUAL.name()).toString();
    }

    /**
     * 根据Id查询
     */
    public String getAuthoriteById(int id)
    {
        StringBuilder sb = new StringBuilder();
        sb = getSelectString(sb);
        return buildSelectStringForAnd(sb, "id", id, SqlKeyWord.Relationship.EQUAL.name()).toString();
    }

    @Override
    public String getCommonColumnsString()
    {
        return SqlColumnsUtil.AUTHORITE_COMMON_COLUMNS;
    }

    @Override
    public StringBuilder getFromTableString(StringBuilder sb)
    {
        return buildString(sb, Enums.TableName.AUTHORITE.getDesc());
    }
}
